package com.sunhaojie.pos.front;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.sunhaojie.pos.common.exception.BussinessRuntimeExceptionFilter;
import com.sunhaojie.pos.common.filters.AccessControlAllowOrignFilter;
import com.sunhaojie.pos.common.filters.LogFilter;
import com.sunhaojie.pos.common.filters.ResetRequestResponseFilter;
import com.sunhaojie.pos.common.interceptor.CorsInterceptor;
import com.sunhaojie.pos.common.interceptor.RequestContextInterceptor;

/**
 *
 * @ClassName BeanConfig
 * @Description Bean 配置
 *
 * @author sunhaojie@kingsoft.com
 * @date 2017年7月24日 上午10:41:33
 */
@Configuration
// @ImportResource(locations = {
// "classpath:spring-dao-pos-core.xml","classpath:spring-rabbit-mq-pos-core.xml"
// })
public class BeanConfig extends WebMvcConfigurerAdapter {

	// private static Logger logger = LoggerFactory.getLogger(BeanConfig.class);

	// @Value("${}")
	// private String redisHost;
	// @Value("${}")
	// private String redisPort;
	// @Value("${}")
	// private String redisPassword;
	//
	// @Bean
	// @ConfigurationProperties(prefix = "spring.redis")
	// public JedisPoolConfig getRedisConfig() {
	// JedisPoolConfig config = new JedisPoolConfig();
	// return config;
	// }
	//
	// @Bean
	// @ConfigurationProperties(prefix = "spring.redis")
	// public JedisConnectionFactory getConnectionFactory() {
	// JedisConnectionFactory factory = new JedisConnectionFactory();
	// JedisPoolConfig config = getRedisConfig();
	// factory.setPoolConfig(config);
	// logger.info("JedisConnectionFactory bean init success.");
	// return factory;
	// }

	// @Bean
	// public RedisTemplate<?, ?> getRedisTemplate() {
	// RedisTemplate<?, ?> template = new StringRedisTemplate(
	// getConnectionFactory());
	// return template;
	// }
    
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
    	
    	CorsInterceptor corsInterceptor = new CorsInterceptor();
    	 registry.addInterceptor(corsInterceptor).addPathPatterns("/**");
    	
        RequestContextInterceptor requestContextInterceptor = new RequestContextInterceptor();
        registry.addInterceptor(requestContextInterceptor).addPathPatterns("/**");
        
        super.addInterceptors(registry);
    }

    @Bean
	public FilterRegistrationBean accessControlAllowOrignFilterRegistration() {
        FilterRegistrationBean exceptionFilterRegistration = new FilterRegistrationBean();
		AccessControlAllowOrignFilter accessControlAllowOrignFilter = new AccessControlAllowOrignFilter();
        exceptionFilterRegistration.setFilter(accessControlAllowOrignFilter);
        exceptionFilterRegistration.addUrlPatterns("/*");
		exceptionFilterRegistration.setOrder(3);
        return exceptionFilterRegistration;
    }

	@Bean
	public FilterRegistrationBean exceptionFilterRegistration() {
		FilterRegistrationBean exceptionFilterRegistration = new FilterRegistrationBean();
		BussinessRuntimeExceptionFilter exceptionFilter = new BussinessRuntimeExceptionFilter();
		exceptionFilterRegistration.setFilter(exceptionFilter);
		exceptionFilterRegistration.addUrlPatterns("/*");
		exceptionFilterRegistration.setOrder(Integer.MAX_VALUE);
		return exceptionFilterRegistration;
	}

    @Bean
    public FilterRegistrationBean resetRequestResponseFilterRegistration() {
        FilterRegistrationBean resetRequestResponseFilterRegistration = new FilterRegistrationBean();
        ResetRequestResponseFilter resetRequestResponseFilter = new ResetRequestResponseFilter();
        resetRequestResponseFilterRegistration.setFilter(resetRequestResponseFilter);
        resetRequestResponseFilterRegistration.addUrlPatterns("/*");
        resetRequestResponseFilterRegistration.setOrder(1);
        return resetRequestResponseFilterRegistration;
    }
    
    @Bean
    public FilterRegistrationBean logFilterRegistration() {
        FilterRegistrationBean logFilterRegistration = new FilterRegistrationBean();
        LogFilter logFilter = new LogFilter();
        logFilterRegistration.setFilter(logFilter);
        logFilterRegistration.addUrlPatterns("/*");
        logFilterRegistration.setOrder(5);
        return logFilterRegistration;
    }
}
